#coding=utf-8

module Aio::Ui
  require "aio/ui/version"
  require "aio/ui/banner"
  require "aio/ui/readable_text"
  require "aio/ui/verbose"
  require "aio/ui/logger"

  class Console

    include Aio::Ui::Verbose

    # Aio::ModuleManager
    attr_accessor :module_manager

    # Aio::DeviceManager
    attr_accessor :device_manager

    # Aio::Parse::Parser
    attr_accessor :parser

    # Aio::Ui::Banner
    attr_accessor :banner

    DefaultCompare = "special/style/compare"

    DefaultInputComapreXML = "input/style/compare_xml"

    def initialize(args)
      self.banner = Banner.new.parse(args)
    end

    def process
      case self.banner.mode
      when 0
        proc_0
      when 1
        proc_1
      end
    end

    # 输出模块表
    def proc_0
      case banner.type
      when "cmd"
        mods = module_manager.get_modules_by_type("cmd")
        print ReadableText.dump_module_cmds(mods)
      when "input"
        mods = module_manager.get_modules_by_type("input")
        print ReadableText.dump_module_inputs(mods)
      when "output"
        mods = module_manager.get_modules_by_type("output")
        print ReadableText.dump_module_outputs(mods)
      else
        mods = module_manager.get_modules_by_type("cmd")
        print ReadableText.dump_module_cmds(mods)

        mods = module_manager.get_modules_by_type("input")
        print ReadableText.dump_module_inputs(mods)

        mods = module_manager.get_modules_by_type("output")
        print ReadableText.dump_module_outputs(mods)
      end
    end

    # 通过选择的输入输出模块进行操作
    def proc_1

      # 当完全没有指定input的时候,是可以跳过的
      if banner.has_input?
        input_klass = module_manager.get_module_klass_by_name(banner.input_style)
        input_klass.input_file = banner.input
        input_klass.ext_info = {:cmds_reg => device_manager.just_cmds_reg}

        parser.input_klass = input_klass
        parser.parse_by_module

        # 当有compare选项的时候，同时解析compare模块，并合并warning信息
        if banner.compare 
          print_good "正在加载比较模块"
          if banner.compare =~ /\.xml$/
            input_klass_compare = module_manager.get_module_klass_by_name(DefaultInputComapreXML)
          else
            print_error "请确认 -c 参数文件后缀"
            banner.banner
          end

          input_klass_compare.input_file = banner.compare
          compare_klass = module_manager.get_module_klass_by_name(DefaultCompare)

          print_good "比较模块加载成功"
          print_good "正在进行对比..."
          parser.input_klass = input_klass_compare
          parser.parse_by_compare(compare_klass)
          print_good "对比分析完成"

        end

        # Debug 处
        if Aio::Base::Debug.ui_enable? #or true
          puts "[Debug UI]"
          device_manager.devices.each_pair{|name, device_klass|
            pp name
            pp device_klass.cmds_useful["show version"]
            #pp device_klass.device_name
            #pp device_klass.cmds_useful
            #pp device_klass.warning_klass.warning_info
            puts
          }
          puts "[/Debug UI]"
        end

        device_manager.warning_summarize_tidy
      end	# 判断没有没 input 尾部

      # NOTE Debug 输出方法处
      #device_manager["PF_YYB_MP2900_1"].inventory

      print_good "正在加载输出模块 #{banner.output_style}"
      output_klass = module_manager.get_module_klass_by_name(banner.output_style)

      pn = Pathname.new(banner.output)
      dir, base = pn.split
      base = base.to_s
      unless base =~ /\./
        suffix = output_klass.file_suffix
        unless suffix == "" or /(?i)null/ =~ suffix
          base << "." + suffix
        end
      end

      print_good "正在输出文件 #{dir + base}"
      output_klass.device_manager = device_manager
      output_klass.module_manager = module_manager
      output_klass.output_file = banner.output
      output_klass.generate
      print_good "输出文件完成"
    end

  end
end
